<template>
  <div class="page" :class="{ editActive: userEdit }">
    <div class="content" :class="{ editActive: userEdit }">
      <div class="tag">
        <div>
          <img class="tagLogo" src="./../assets/可追溯.png" alt="" />源头可追溯
        </div>
        <div>
          <img class="tagLogo" src="./../assets/可追究.png" alt="" />责任可追究
        </div>
        <div>
          <img class="tagLogo" src="./../assets/可查.png" alt="" />消费者可查
        </div>
      </div>
      <div class="main">
        <div class="suyuanTit">溯有源 食为安</div>
        <div class="Titcontent">全生命周期质量溯源平台</div>
        <div class="tabBody" :class="{ tabBodyActive: !userEdit }">
          <van-sticky :offset-top="0">
            <div class="tabNav">
              <van-tabs background="#e8f8ef" v-model="active" color="#9BA5A2" line-width="30px">
                <van-tab
                  title="产品信息"
                  :replace="true"
                  :to="`/productRecord?tsId=${id}&baseId=${baseId}&userId=${userId}&tabval=0`"
                >
                </van-tab>
                <van-tab
                  title="农事记录"
                  :replace="true"
                  :to="`/growthRecord?tsId=${id}&baseId=${baseId}&userId=${userId}&tabval=1`"
                >
                </van-tab>

                <van-tab
                  title="农资投入"
                  :replace="true"
                  :to="`/investment?tsId=${id}&baseId=${baseId}&userId=${userId}&tabval=2`"
                ></van-tab>
                <van-tab
                  title="农情记录"
                  :replace="true"
                  :to="`/situation?tsId=${id}&baseId=${baseId}&userId=${userId}&tabval=3`"
                ></van-tab>
                <van-tab
                  title="权威检测"
                  :replace="true"
                  :to="`/testing?tsId=${id}&baseId=${baseId}&userId=${userId}&tabval=4`"
                ></van-tab>
              </van-tabs>
            </div>
          </van-sticky>

          <router-view></router-view>
        </div>
      </div>
    </div>

    <div class="suyuanEdit" v-if="userEdit">
      <div class="del" @click="delsuyuan()">
        <van-icon name="delete-o" color="red" size="18px" />
      </div>
      <div class="editBtn" @click="edit()">编辑</div>
    </div>
  </div>
</template>

<script>
import wx from 'weixin-js-sdk'
import { Dialog, Toast } from 'vant'
// import { getTracingNumApi, delTracingApi } from '@/api/tracing'
import { delTracingApi } from '@/api/tracing'
export default {
  name: 'ShuzhongtianH5Index',

  data () {
    return {
      active: 0,
      id: '',
      isSticky: false,
      userEdit: false,
      time: '',
      baseId: '',
      userId: ''
    }
  },

  mounted () {
    this.id = this.$route.query.tsId
    this.baseId = this.$route.query.baseId
    this.userId = this.$route.query.userId // 有userid开启编辑功能
    this.active = +this.$route.query.tabval // 路由给tab值
    if (this.userId === '' || this.userId === undefined) {
      this.userId = ''
      this.userEdit = false
    } else {
      this.userEdit = true
    }
    // 监听浏览器的返回按钮点击事件
    window.addEventListener('popstate', this.handleBackButton)
    this.getCurrentDate()
    if (this.userEdit !== true) {
      // this.tracingNum()
    }
  },

  beforeDestroy () {
    // 在组件销毁前移除监听器，避免内存泄漏
    window.removeEventListener('popstate', this.handleBackButton)
  },
  methods: {
    // 处理返回按钮点击事件
    handleBackButton () {
      wx.miniProgram.navigateBack({})
    },
    // 溯源次数加一
    // tracingNum () {
    //   getTracingNumApi({ time: this.time, id: this.id, baseId: this.baseId })
    // },
    // 获取当前年月日
    getCurrentDate () {
      const today = new Date()
      this.time = `${today.getFullYear()}-${String(today.getMonth + 1).padStart(
        2,
        '0'
      )}-${String(today.getDate()).padStart(2, '0')}`
    },
    // 删除溯源
    delsuyuan () {
      Dialog.confirm({
        message: '确定删除该溯源档案',
        confirmButtonColor: '#F76560',
        cancelButtonColo: '#19B560'
      })
        .then(async () => {
          // on confirm
          const res = await delTracingApi(this.id)
          if (res.code === 200) {
            Toast('删除成功')
            this.handleBackButton()
          } else {
            Toast('删除失败')
          }
        })
        .catch(() => {
          // on cancel
        })
    },
    edit () {
      this.$router.push({
        path: `/edit?tsId=${this.id}&baseId=${this.baseId}&userId=${this.userId}`
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.page {
  width: 375px;
  height: 100vh;
  overflow: hidden;
  position: relative;
}
.editActive {
  padding-bottom: 62px;
}
.content {
  width: 375px;
  height: 100vh;
  overflow-y: auto;
}
.tag {
  width: 375px;
  display: flex;
  justify-content: space-around;
  margin: 12px 0;
  overflow: hidden;
}
.main {
  width: 375px;
  // height: 1192px;
  background: #147967;
  border-radius: 8px 8px 8px 8px;
  padding: 28px 16px;
  overflow: hidden;
  position: relative;
  background-image: url(~@/assets/maskGroup.png);
  background-size: 100% auto;
  background-position: right top;
  background-repeat: no-repeat;
  .suyuanTit {
    width: 180px;
    height: 28px;
    font-family: PingFang SC, PingFang SC;
    font-weight: 500;
    font-size: 24px;
    color: #ffffff;
    line-height: 28px;
    text-align: left;
    font-style: normal;
    text-transform: none;
    letter-spacing: 2.8px;
  }
  .Titcontent {
    width: 180px;
    height: 24px;
    font-family: PingFang SC, PingFang SC;
    font-weight: 400;
    font-size: 12px;
    color: #ffffff;
    line-height: 24px;
    text-align: left;
    font-style: normal;
    text-transform: none;
    margin: 4px 0 24px;
    letter-spacing: 3.5px;
  }
  .tabBody {
    width: 343px;
    min-height: 550px;
    background: #E7E7E7;
    border-radius: 8px;
    overflow: hidden;
  }
  .tabBodyActive {
    min-height: 632px !important;
  }
}
.suyuanEdit {
  width: 375px;
  height: 82px;
  border-radius: 8px 8px 0px 0;
  background: #fff;
  position: absolute;
  bottom: 0;
  left: 0;
  z-index:1;
  display: flex;
  align-items: center;
  padding-bottom: 32px;
  padding-left: 34px;
  .del {
    margin-right: 22px;
  }
  .editBtn {
    width: 291px;
    height: 40px;
    background: #19b560;
    border-radius: 24px 24px 24px 24px;
    text-align: center;
    line-height: 40px;
    color: #fff;
    font-size: 16px;
  }
}
.tag {
  font-size: 12px;
}
.tagLogo {
  vertical-align: text-top;
}
::v-deep .van-tab {
  font-size: 12px!important;
}
</style>
